package com.gxtbindings.bindings.client;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;

public class BeanModelConverter<E> implements ListStoreConverter<E, BeanModel> {

	private BeanModelFactory factory;
	
	public BeanModelConverter() {
	}
	
	@SuppressWarnings("unchecked")
	public E convertModelValue(BeanModel model) {
		Object obj = model.getBean();
		return (E)obj;
	}

	public BeanModel convertObjectToModel(E object) {
		if (factory == null)
		{
			factory = BeanModelLookup.get().getFactory(object.getClass());
		}
		
		return factory.createModel(object);
	}
}
